/*
 * 版权所有 (C) 2001-2014 深圳市艾派应用系统有限公司。保留所有权利。
 * 版本：
 * 修改记录：
 *		1、2014-11-26，tfl创建。 
 */
package com.chinamobile.ln.ictenterprises.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.chinamobile.ln.ictenterprises.R;
import com.chinamobile.ln.ictenterprises.common.Config;
import com.chinamobile.ln.ictenterprises.common.Constants;
import com.chinamobile.ln.ictenterprises.common.GlobalSetting;
import com.chinamobile.ln.ictenterprises.http.HttpAsyncTask;
import com.chinamobile.ln.ictenterprises.model.Step;
import com.chinamobile.ln.ictenterprises.model.request.HistoryDetail;
import com.chinamobile.ln.ictenterprises.model.response.ResponseHistoryDetail;
import com.chinamobile.ln.ictenterprises.model.response.ResponseHistoryDetail.ReceiveResult;
import com.chinamobile.ln.ictenterprises.utils.JsonUtil;
import com.chinamobile.ln.ictenterprises.utils.Utils;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

@SuppressLint("HandlerLeak")
public class MsgDetailActivity extends ParentActivity implements
    OnClickListener
{
    private ImageView btn_back;
    private TextView content_txt, send_time, timer_time;
    private boolean isSendOver = false;
    private String id, content, timerTime, sendTime, addressList;
    private ListView listView;
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private List<ReceiveResult> results;
    private LinearLayout ly_send_time;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        init(R.layout.msg_detail_layout);
    }

    @Override
    protected void setupView()
    {
        btn_back = (ImageView) findViewById(R.id.back_btn);
        btn_back.setOnClickListener(this);
        content_txt = (TextView) findViewById(R.id.content_txt);
        send_time = (TextView) findViewById(R.id.send_time);
        timer_time = (TextView) findViewById(R.id.timer_time);
        listView = (ListView) findViewById(R.id.address_list);
        ly_send_time = (LinearLayout) findViewById(R.id.ly_send_time);
    }

    @Override
    protected void setupData()
    {
        Intent intent = getIntent();
        isSendOver = intent.getBooleanExtra("isSendOver", false);
        id = intent.getStringExtra("id");
        // 发送中
        if (!isSendOver)
        {
            content = intent.getStringExtra("content");
            timerTime = intent.getStringExtra("time");
            addressList = intent.getStringExtra("addressList");
            String[] str = addressList.split(",");
            if (str.length > 0)
            {
                for (int i = 0; i < str.length; i++)
                {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("address", str[i] + "、");
                    list.add(map);
                }
            }
            setView();
        }
        // 已发送
        else
        {
            ly_send_time.setVisibility(View.VISIBLE);
            if (!checkNet(context))
            {
                showTip(R.string.network_fail);
                return;
            }
            HistoryDetail historyDetail = new HistoryDetail(
                GlobalSetting.GET_SNET_BY_ID, id);
            String params = JsonUtil.objectToJson(historyDetail);
            Step step = Utils.paramsUtil(Config.SEND_MSG, params);
            showProgressDialog("正在获取详情...");
            HttpAsyncTask httpAsyncTask = new HttpAsyncTask(step,
                HttpAsyncTask.CACHE_TYPE_ACTION, sharedPref.getString(
                    Constants.COOKIES, ""), GlobalSetting.HISTORY_DETAIL);
            httpAsyncTask.setUIHandler(getDetailHandler);
            mHttpManager.execute(httpAsyncTask);
        }
    }

    private Handler getDetailHandler = new Handler() {

        public void handleMessage(Message msg)
        {
            if (msg.obj != null)
            {
                @SuppressWarnings("unchecked")
                Map<String, Object> httpMap = (Map<String, Object>) msg.obj;
                int reultCode = (Integer) httpMap
                    .get(GlobalSetting.STUATUS_CODE);
                if (reultCode == GlobalSetting.STUATUS_CODE_SUCCESS)
                {
                    ResponseHistoryDetail responseHistoryDetail = (ResponseHistoryDetail) httpMap
                        .get(GlobalSetting.JSON_HISTORY_DETAIL);
                    if (responseHistoryDetail != null)
                    {
                        if (responseHistoryDetail.resultFlag
                            .equals(GlobalSetting.RESULT_FLAG_SUC))
                        {
                            content = responseHistoryDetail.results.msg;
                            timerTime = responseHistoryDetail.results.timingTime;
                            sendTime = responseHistoryDetail.results.sendTime;
                            results = responseHistoryDetail.results.receiveResult;
                            if (results.size() > 0 && results != null)
                            {
                                for (ReceiveResult result : results)
                                {
                                    Map<String, Object> map = new HashMap<String, Object>();
                                    map.put("address", result.phone + "("
                                        + result.result + ")" + "、");
                                    list.add(map);
                                }
                            }
                            setView();
                        }
                        else
                        {
                            showTip(R.string.get_detail_fail);
                        }
                    }
                    else
                    {
                        showTip(R.string.get_detail_fail);
                    }
                }
                else if (reultCode == GlobalSetting.STUATUS_CODE_INVALIDATION)
                {
                    showTip(R.string.session_timeout);
                    Intent intent = new Intent();
                    intent
                        .setClass(MsgDetailActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
                else if (reultCode == GlobalSetting.STUATUS_CODE_TIMEROUT)
                {
                    showTip(R.string.network_error);
                }
            }
            else
            {
                showTip(R.string.get_detail_fail);
            }
            dismissProgressDialog();
        };
    };

    private void setView()
    {
        content_txt.setText(content);
        timer_time.setText(timerTime);
        send_time.setText(sendTime);
        SimpleAdapter adapter = new SimpleAdapter(context, list,
            R.layout.history_detail_item, new String[] { "address" },
            new int[] { R.id.tv_address });
        listView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v)
    {
        finish();
    }
}
